<?php
namespace Admin\Controller;

use Think\Controller;

class LoanManageController extends CommenController
{
	public function overdue_list(){
		$shop_identifier = $_SESSION['shop_identifier'];
		// $shop_identifier = '0100010020';
		$where = array();
		$user_overdue = M('user_overdue','','XYC_SERVICE');
		$user = M("user",'','XYC');
		$merchant = M("merchant",'','XYC');
		$content = trim(I("get.content"));
        $start = I("get.start");
        $end = I("get.end");
        $start = strtotime($start);
        $end = strtotime($end);
        if($start&&$end){
            $where["a.ctime"]=array('between',array($start,$end));
        }elseif ($start){
            $where["a.ctime"]=array('egt',$start);
        }elseif ($end){
            $where["a.ctime"]=array('elt',$end);
        }
        if($content){
            $where["username|mobile"] = array('like',"$content%");
        }
        $where['a.shop_identifier']=$shop_identifier;
        $where['a.status']=1;
        $search_arr=array('start'=>I("get.start"),'end'=>I("get.end"),'content'=>$content);
		$tol = $user_overdue->alias('a')->where($where)->count();
        $row = '15';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field='a.user_id,a.numbering,a.repayment_money,a.should_time,a.overdue_day,a.overdue_money,a.ctime';
        $list = $user_overdue->alias('a')->join('join ')->where($where)->limit($page->listfirst, $page->listRows)->order("ctime DESC")->field($field)->select();
        //借款人
        $lender=M('vz_loan','','XYC')->join('a join lender b on b.id=a.lender_id')->where(['a.order_number'=>['in',array_column($list,'numbering')]])->getfield('a.order_number,b.name');
        foreach($list as $k=>$v){
            $user_id = $v['user_id'];
            $userData = $user->where(array('id'=>$user_id))->field('username,mobile')->find();
            $list[$k]['username'] = $userData['username'];
            $list[$k]['admname'] = M('shopkeeper','',"XYC")->where(array('id'=>$v['ua_id']))->getField('name');
            $list[$k]['mobile'] = $userData['mobile'];
            $list[$k]['ctime'] = $v['ctime']?date('Y-m-d',$v['ctime']):'';
            $list[$k]['should_time'] = $v['should_time']?date('Y-m-d',$v['should_time']):'';
            $remark = M('overdue_remark','',"XYC")->where(array('numbering'=>$v['numbering']))->order('ctime DESC')->select();
            $list[$k]['final_time'] = $remark[0]['ctime']?date('Y-m-d',$remark[0]['ctime']):'';
            $list[$k]['remarkcount'] = count($remark);
            $list[$k]['lender']=$lender[$v['order_number']];
        }
        $this->assign(array(
            'list' => $list,
            'fpage' => $fpage,
            'tol' => $tol,
            'search'=>$search_arr
        ));
		$this->display();
	}

	public function add_remark(){
		if(IS_POST){
			$time=strtotime(I('time'));
			$remark=I('remark');
			$numbering=I('numbering');
			$res = M('overdue_remark','','XYC')->add(array('numbering'=>$numbering,'content'=>$remark,'next_time'=>$time,'ctime'=>time()));
			if($res){
                add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],"添加了催记",$type='3');
                $msg["code"] = 1001;
                $msg["result"] = "催记添加成功";
                $this->ajaxReturn($msg);
            }else{
                $msg["code"] = 1002;
                $msg["result"] = "催记添加失败";
                $this->ajaxReturn($msg);
            }
		}
		$this->display();
	}

	public function remark_record(){
		$numbering = I("numbering");
		if($numbering){
			$overdue_remark = M("overdue_remark",'','XYC');
			$where["numbering"] = $numbering;
			$list = $overdue_remark->where($where)->order("ctime DESC")->limit(20)->select();
			foreach($list as $k=>$v){
				$list[$k]["ctime"] = date("Y-m-d",$v['ctime']);
				$list[$k]["next_time"] = date("Y-m-d",$v['next_time']);
			}
		}
		$this->assign(array(
            'list' => $list
        ));
		$this->display();
	
	}

	public function get_more_record(){
		$numbering = I("numbering");
        $m_c_num = I("m_c_num");
        if($numbering){
            $num = 20*$m_c_num-20;
            $overdue_remark = M("overdue_remark",'','XYC');
			$where["numbering"] = $numbering;
			$list = $overdue_remark->where($where)->order("ctime DESC")->limit("$num,20")->select();
            foreach($list as $k=>$v){
				$list[$k]["ctime"] = date("Y-m-d",$v['ctime']);
				$list[$k]["next_time"] = date("Y-m-d",$v['next_time']);
			}
            $msg["code"] = 1001;
            $msg["result"] = $list;
            $this->ajaxReturn($msg);
        }else{
            $msg["code"] = 1002;
            $msg["result"] = "账户异常！";
            $this->ajaxReturn($msg);
        }
	}

    public function set_dead_bill(){
        $id = I('id');
        $numbering = I("numbering");
        if($numbering){
            $user_overdue = M('user_overdue','','XYC_SERVICE');
            $vz_loan = M("vz_loan",'','XYC');
            $res = $vz_loan->where(array('order_number'=>$numbering))->save(array('status'=>6));
            $res = $user_overdue->where(array('id'=>$id))->save(array('type'=>2));

            $msg["code"] = 1001;
            $msg["result"] = $list;
            $this->ajaxReturn($msg);
        }else{
            $msg["code"] = 1002;
            $msg["result"] = "账户异常！";
            $this->ajaxReturn($msg);
        }
    }

    /**
     *  //逾期订单分配
     */

    public  function overdue_operate(){

        $s_time=I('should_time');
        $show_num = I('show_num');
        $search = I('key');//搜索

        if($s_time){
            $apply_arr = explode(' - ', $s_time);
            $a_start = strtotime($apply_arr[0]);
            $a_end = strtotime($apply_arr[1]);
            $where['should_time']=['between',[$a_start,$a_end]];
        }


        $db_overdue=M('user_overdue','','XYC_SERVICE');
        $where['shop_identifier']= $_SESSION['shop_identifier'];
        $where['status']=1;
        $tol = $db_overdue->where($where)->COUNT();
        $row = '10';

        if (!empty($show_num)) {
            $row = $show_num;
        }
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data= $db_overdue->where($where)->field('id,numbering,user_id,repayment_money,overdue_day,overdue_money,should_time')->select();
        $user=M('user','','XYC')->where(['id'=>['in',array_column($data,'user_id')]])->getfield('id,username,mobile',true);
        foreach ($data as $k=>$v){
            $data[$k]['username']=$user[$v['user_id']]['username'];
            $data[$k]['mobile']=$user[$v['user_id']]['mobile'];
            $data[$k]['should_time']=date('Y-m-d',$v['should_time']);
        }

        $search_arr = array('key' => $search, 'should_time' => $s_time, 'show_num' => $show_num);

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'search' => $search_arr,
            'tol' => $tol
        ));


        $this->display();
    }

    /***
     * 逾期用户分配给催收员
     */
    public function user_distribute()
    {
        $overdue_id = I('id');
        $overdue_arr = explode(',', $overdue_id);
        if (empty($_POST)) {
            $user_arr= M('user_overdue','','XYC_SERVICE')->where(['id'=>['in',$overdue_arr]])->getfield('user_id');
            $user = M('user', '', 'XYC')->where(['id' => array('in', $user_arr)])->field('mobile')->select();
            //具有审核权限的子账号
//            $check=M('shopkeeper','','XYC')->where(['rid'=>7])->field('id,name')->select();
            $check = M('shopkeeper', '', 'XYC')->join('a join shopkeeper_role b on b.sid=a.id and shop_identifier= ' . $_SESSION['shop_identifier'])->where(['b.rid' => 12,'a.state'=>1])->field('a.id,a.name')->select();
            $this->assign(array('user' => $user, 'check' => $check, 'overdue_id' => $overdue_id));
            $this->display();
            die;
        }
        $check_id = I('check_id');
        $arr = array('ua_id' => $check_id);
        $update = M('user_overdue', '', 'XYC_SERVICE')->where(['id' => array('in', $overdue_arr)])->save($arr);
        if ($update) {
            add_control_log($_SESSION['shop_identifier'], $_SESSION['name'], '【逾期管理-逾期分配】分配逾期-' . $overdue_arr, 3);
            $this->ajaxReturn(array('code' => '1001', 'result' => '分配成功'));
            die;
        }
        $this->ajaxReturn(array('code' => '1002', 'result' => '分配失败'));
        die;

    }
}